package cn.edu.nwsuaf.examcenter.service.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import cn.edu.nwsuaf.examcenter.model.PageModel;
import cn.edu.nwsuaf.examcenter.pojo.CourceEmployee;
import cn.edu.nwsuaf.examcenter.pojo.CourceEmployeeId;
import cn.edu.nwsuaf.examcenter.service.CourceEmployeeService;
import cn.edu.nwsuaf.examcenter.model.StudentScoreModel;

@Service(value="courceEmployeeService")
public class CourceEmployeeServiceImpl extends BaseServiceImpl<CourceEmployee> implements CourceEmployeeService {

	@Override
	public String addCourceEmployee(String teacherNo, String sclassNo, String courceNo) {
		// TODO Auto-generated method stub
		CourceEmployee courceEmployee = new CourceEmployee();
		CourceEmployeeId id = new CourceEmployeeId();
		id.setClassNo(sclassNo);
		id.setCourceNo(courceNo);
		id.setTeacherNo(teacherNo);
		courceEmployee.setId(id);
		super.saveData(courceEmployee);
		return "success";
	}

	@Override
	public void loadCourceEmployee(ModelMap modelMap) {
		// TODO Auto-generated method stub
		PageModel<CourceEmployee> pageModel = new PageModel<>();
		List<CourceEmployee> list = super.findAll(CourceEmployee.class);
		pageModel.setTotalrows(list.size());
		pageModel.setList(list);
		modelMap.addAttribute("pageModel",pageModel);
	}

	@Override
	public String deleteCourceEmployee(CourceEmployee courceEmployee) {
		// TODO Auto-generated method stub
		super.deleteData(courceEmployee);
		return "success";
	}

	@Override
	public CourceEmployee jsonLoadEmployee(CourceEmployeeId id) {
		// TODO Auto-generated method stub
		CourceEmployee p = super.getById(CourceEmployee.class, id);
		Hibernate.initialize(p.getClass());
		Hibernate.initialize(p.getCource());
		Hibernate.initialize(p.getTeacher());
		return p;
	}

	@Override
	public List<String> getSclassNoByCourceNo(String courceNo) {
		// TODO Auto-generated method stub
		List<String> classNoList = new ArrayList<>();
		
		String hql = "FROM CourceEmployee as ce where ce.cource.courceNo=?";
		String[] param = {courceNo};
		List<CourceEmployee> sclassList = super.findAll(hql, param);
		for(CourceEmployee c:sclassList){
			classNoList.add(c.getSclass().getClassNo());
		}
		return classNoList;
	}
	@Override
	public void getMyCource(String sclassNo,List<String> courceList, List<StudentScoreModel> modelList) {
		// TODO Auto-generated method stub
		String hql = "FROM CourceEmployee where sclass.classNo=?";
		String[] param = {sclassNo};
		List<CourceEmployee> list = super.findAll(hql, param);
		//System.out.println(list.size());
		for(CourceEmployee ce:list){
			StudentScoreModel model = new StudentScoreModel();
			model.setCourceName(ce.getCource().getCourceName());
			model.setCourceScore(ce.getCource().getCourceScore());
			courceList.add(ce.getCource().getCourceNo());
			modelList.add(model);
		}
	}
}
